package com.itheima.mall.api.sms;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.mall.mapper.sms.CouponHistoryMapper;
import com.itheima.mall.pojo.sms.CouponHistory;
import com.itheima.mall.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

@DubboService
public class CouponHistoryServiceApiImpl implements CouponHistoryServiceApi{
    @Autowired
    private CouponHistoryMapper couponHistoryMapper;

    //根据优惠券id，使用状态，订单编号分页获取领取记录
    @Override
    public PageResult<CouponHistory> list(Integer couponId, String orderSn, Integer pageNum, Integer pageSize, Integer userStatus) {
        Page<CouponHistory> pageInfo=new Page(pageNum,pageSize);
        LambdaQueryWrapper<CouponHistory>wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(couponId !=null,CouponHistory::getCouponId,couponId);
        wrapper.eq(orderSn !=null,CouponHistory::getOrderSn,orderSn);
        Page<CouponHistory> historyPage = couponHistoryMapper.selectPage(pageInfo, wrapper);
        PageResult pageResult=new PageResult();
        pageResult.setList(historyPage.getRecords());
        pageResult.setPageNum(Convert.toInt(historyPage.getCurrent()));
        pageResult.setPageSize(Convert.toInt(historyPage.getSize()));
        pageResult.setTotal(Convert.toInt(historyPage.getTotal()));
        pageResult.setTotalPage(Convert.toInt(historyPage.getPages()));
        return pageResult;
    }
}
